﻿using DataprotoBuf;
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp_SignalR_client
{
    class Program : IClientConsloe
    {
        static HubConnection _connection;
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! SignalR_client");
            CreateConncetion();
            ReconnectServer();


            var t1= _connection.SendAsync("SendMessageAsync", "客户端第一次调用");
             Console.WriteLine("服务端的状态："+t1.AsyncState);
            var t2 =_connection.InvokeAsync<string>("SendMessageAsync", "客户端第二次调用");
            Console.WriteLine("服务端返回的结果："+t2.Result);

            Console.WriteLine("请输入要给所有客户端发送信息：");
            string userInputStr = Console.ReadLine();
            _connection.InvokeAsync("CallClientByAllAsync", userInputStr);

            Console.ReadKey();
        }

        private static void CreateConncetion()
        {
            _connection = new HubConnectionBuilder()
                       .WithUrl("http://localhost:5000/mychat")
                        //.WithUrl("http://127.0.0.1:8008/camera")
                       //.WithUrl("http://127.0.0.1:8001/hubs/fr")
                       .Build();

            _connection.Closed += SignalRClosedAsync;
            _connection.On<string>("SendMsg", msg =>
            {
                Console.WriteLine($"{DateTime.Now} Recv Message :{msg}");
            });
            _connection.On<string>("ShowMessageAsync", ShowMessageAsync1);

            _connection.On<byte[]>("UpdateUICameraImage", buf =>
            {
                Console.WriteLine(buf);
            });

            _connection.On<Class1>("RefreshTime", buf =>
            {
                Console.WriteLine(buf);
            });
        }

        public static void ReconnectServer()
        {
            Console.WriteLine($"{DateTime.Now} 链接中断,正在尝试链接...");
        lab:
            try
            {

                _connection?.DisposeAsync();
                _connection = null;
                CreateConncetion();
                _connection.StartAsync().Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{DateTime.Now} ex:{ex.Message}");
                Thread.Sleep(1000);
                goto lab;
            }

            Console.WriteLine($"{DateTime.Now} 通信恢复正常。");
        }

        private async static Task SignalRClosedAsync(Exception arg)
        {
            await Task.Delay(TimeSpan.FromSeconds(2));
            Console.WriteLine($"{DateTime.Now} SignalRClosedAsync");
        }

        public static async Task ShowMessageAsync1(string msg)
        {
            await Task.Delay(10);
            Console.WriteLine(msg);
        }

        //实际这个方法可以  约定客户端的接口也是为了，让该方法在此客户端有，但是碰上了客户端是可控制台需要静态方法，
        //然后就有点化蛇填脚丫子
        public async Task ShowMessageAsync(string msg)
        {
            await Task.Delay(10);
            Console.WriteLine(msg);
        }
    }
}
